package com.power.services.doctors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.doctor.TiDoctorsDAO;
import com.power.model.department.TiDepartment;
import com.power.model.doctor.TiDoctors;
import com.power.model.hospital.TiHospital;
import com.power.util.Assent;
import com.power.util.PageUtil;

@Service("tiDoctorsService")
public class TiDoctorsServiceImpl implements TiDoctorsService {

	@Autowired
	private TiDoctorsDAO tiDoctorsDAO;

	public void create(TiDoctors doctors) {
		this.tiDoctorsDAO.save(doctors);
	}

	public TiDoctors read(Integer id) {
		return ((TiDoctors) this.tiDoctorsDAO.load(id));
	}

    public void saveOrUpdate(TiDoctors doctors) {
    	tiDoctorsDAO.saveOrUpdate(doctors);
    }

    public void delete(String ids){
        String[] idAry = ids.split("_");
        for (int i = 0; i < idAry.length; i++) {
            Integer id = new Integer(idAry[i]);
            tiDoctorsDAO.deleteByKey(id);
        }
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> findAll(String doctorsName, int start, int limit, Integer hid,Integer did,Integer gid) {
        List<TiDoctors> list = null;
        Map<String, Object> map = new HashMap<String, Object>();
        DetachedCriteria criteria = DetachedCriteria.forClass(TiDoctors.class);
        if(!Assent.checkNullAndEmpty(doctorsName))
            criteria.add(Restrictions.like("realName", "%"+doctorsName+"%"));
        if(hid !=-1)
        criteria.add(Restrictions.eq("hid", hid));
        if(did !=-1)
        criteria.add(Restrictions.eq("did", did));
        if(gid !=-1)
        criteria.add(Restrictions.eq("gid", gid));
        list = tiDoctorsDAO.findByCriteria(criteria, start, limit);
        int count = tiDoctorsDAO.getRowCount(criteria);
        if(null == list)
            list = new ArrayList<TiDoctors>();
        map.put("data", list);
        map.put("count", count);
        return map;
    }

    public String getProName(Integer code) {
    	String sql="SELECT a.name FROM t_s_district t ,t_s_district a WHERE t.upper_id = a.id AND t.id="+code;
    	List list = tiDoctorsDAO.findBySQL(sql);
    	String proName = "";
    	if(list.size()>0)
    		proName = list.get(0).toString();
    	return proName ;
    }
    
    public String getProId(Integer code) {
    	String sql="SELECT a.id FROM t_s_district t ,t_s_district a WHERE t.upper_id = a.id AND t.id="+code;
    	List list = tiDoctorsDAO.findBySQL(sql);
    	String proName = "";
    	if(list.size()>0)
    		proName = list.get(0).toString();
    	return proName ;
    }
    
    public String setDistrictName(Integer code) {
    	String sql="SELECT t.name FROM t_s_district t WHERE t.id = "+code;
    	List list = tiDoctorsDAO.findBySQL(sql);
    	String cityName = "";
    	if(list.size()>0)
    		cityName = list.get(0).toString();
    	return cityName ;
    }
    
    public void delete(TiDoctors arg0) {
    	tiDoctorsDAO.delete(arg0);
    }

    public void update(TiDoctors arg0) {
    	tiDoctorsDAO.update(arg0);
    }

	public void deleteHos(String ids) {
		String[] idAry = ids.split("_");
        for (int i = 0; i < idAry.length; i++) {
            Integer id = new Integer(idAry[i]);
            tiDoctorsDAO.executeSql("update t_i_doctors set deleteFlag=1 where id="+id);
        }
	}

	@SuppressWarnings("unchecked")
	public List<TiDoctors> findByGroupId(Integer gid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TiDoctors.class);
		criteria.add(Restrictions.eq("gid", gid));
		return tiDoctorsDAO.findByCriteria(criteria);
	}

	public PageUtil searchDoctor(String keyword, int pageNo, int limit) {
		PageUtil pu = new PageUtil();
		DetachedCriteria criteria = DetachedCriteria.forClass(TiDoctors.class);
		criteria.add(Restrictions.like("realName", "%" + keyword + "%"));
		criteria.addOrder(Order.asc("firstChar"));
		List<TiDoctors> list = tiDoctorsDAO.findByCriteria(criteria, (pageNo-1)*limit, limit);
		pu.setList(list);
		pu.setPageNo(pageNo);
		pu.setPageSize(limit);
		pu.setTotalRecords(tiDoctorsDAO.getRowCount(criteria));
		return pu;
	}

	public List<TiDoctors> findBySearchKeyWord(String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TiDoctors.class);
		criteria.add(Restrictions.like("realName", "%" + keyword + "%"));
		criteria.addOrder(Order.asc("firstChar"));
		List<TiDoctors> list = tiDoctorsDAO.findByCriteria(criteria, 0, 10);
		return list;
	}


}